package com.vv.vv_commonlibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;
import android.widget.EditText;
import android.widget.Toast;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 作者：huangxijun
 * 时间：2018/11/3.
 * 说明：禁止输入表情
 */

public class IntPutFilter {
    /**
     * 禁止输入表情，限制字数的
     */
    public static void intPutFilter(EditText editText, int s) {
        InputFilter inputFilter = new InputFilter() {
            Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Matcher emojiMatcher = emoji.matcher(source);
                if (emojiMatcher.find()) {
//                Toast.makeText(,"不支持输入表情", 0).show();
//                    CustomToast.showToast("不支持输入表情");
                    return "";
                }
                return null;
            }
        };
        editText.setFilters(new InputFilter[]{inputFilter, new InputFilter.LengthFilter(s)});
    }

    /**
     * 禁止输入表情，不限制字数的
     */
    public static void intPutFilter_no(final Context context, EditText editText) {
        InputFilter inputFilter = new InputFilter() {
            Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Matcher emojiMatcher = emoji.matcher(source);
                if (emojiMatcher.find()) {
                    Toast.makeText(context, "不支持输入表情", Toast.LENGTH_SHORT).show();
                    return "";
                }
                return null;
            }
        };
        editText.setFilters(new InputFilter[]{inputFilter});
    }

    /**
     * 禁止输入表情，不限制字数的
     * 过滤非选中输入，比如输入中文时，过滤掉搜索框里已经输入的拼音（小米手机出现的情况）
     * 返回空字符串，就代表匹配不成功，返回null代表匹配成功
     */
    public static void control_input(EditText editText) {
        InputFilter inputFilter = new InputFilter() {
            Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Matcher emojiMatcher = emoji.matcher(source);
                if (emojiMatcher.find()) {
                    //                    Toast.makeText(MainActivity.this,"不支持输入表情", 0).show();
//                    CustomToast.showToast("不支持输入表情");
                    return "";
                }
                SpannableString ss = new SpannableString(source);
                Object[] spans = ss.getSpans(0, ss.length(), Object.class);
                if (spans != null) {
                    for (Object span : spans) {
                        if (span instanceof UnderlineSpan) {
                            return "";
                        }
                    }
                }
                return null;
            }
        };
        editText.setFilters(new InputFilter[]{inputFilter});
    }

    /**
     * 输入的最大值和最小值以及小数点值位数
     */
    public static void controlNumberInput(EditText editText, final float min, final float max) {
        InputFilter inputFilter = new InputFilter() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                try {
                    //限制小数点位数
                    if (source.equals(".") && dest.toString().length() == 0) {
                        return "0.";
                    }
                    if (dest.toString().contains(".")) {
                        int index = dest.toString().indexOf(".");
                        int mlength = dest.toString().substring(index).length();
                        if (mlength == 3) {
                            return "";
                        }
                    }
                    //限制大小
                    float input = Float.valueOf(dest.toString() + source.toString());
                    if (isInRange(min, max, input)) {
                        return null;
                    }
                } catch (Exception nfe) {
                }
                return "";
            }

            private boolean isInRange(float a, float b, float c) {
                return b > a ? c >= a && c <= b : c >= b && c <= a;
            }
        };
        editText.setFilters(new InputFilter[]{inputFilter});
    }

}
